package nowcoder.vivo2;

import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int maxWeight = sc.nextInt();
        sc.nextLine();
        String l1 = sc.nextLine();
        String l2 = sc.nextLine();
        String[] ws = l1.split(",");
        String[] vs = l2.split(",");
        int[] w = new int[ws.length];
        int[] v = new int[vs.length];
        for(int i = 0; i < w.length; i++){
            w[i] = Integer.parseInt(ws[i]);
            v[i] = Integer.parseInt(vs[i]);
        }

        System.out.println(getMaxV(maxWeight, v, w));
    }

    private static int getMaxV(int max, int[] v, int[] w){
        int n = v.length;
        int[][] dp = new int[n+1][max+1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= max; j++){
                if(j < w[i-1]){
                    dp[i][j] = dp[i-1][j];
                }else{
                    dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-w[i-1]]+v[i-1]);
                }
            }
        }
        return dp[n][max];
    }

}
